Completed
Push — master ( 2e0c84...963a90 )
by Ankit
02:46
created

index.js ➔ firstConversation   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
var flag = 0;
2
var pre = "";
3
4
// Websocket Connection Open
5
var conn = new WebSocket("ws://localhost:8080");
0 ignored issues
show
Bug introduced by
The variable WebSocket seems to be never declared. If this is a global, consider adding a /** global: WebSocket */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
conn.onopen = function () {
7
  // console.log("Connection established!");
8
  init();
9
};
10
11
// On Message
12
conn.onmessage = function(e) {
13
  var msg = JSON.parse(e.data);
14
  console.log(msg);
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...
15
  if (!width())
16
  {
17
    SideBar(msg.sidebar);
18
  }
19
  else
20
  {
21
    if( document.getElementById("conversation").style.display == "none")
22
    {
23
      SideBar(msg.sidebar);
24
    }
25
  }
26
27
  if (msg.initial !== undefined)
28
  {
29
    SideBar(msg.initial);
30
  }
31
32
  if (msg.conversation !== undefined)
33
  {
34
    updateConversation(msg.conversation);
35
  }
36
37
  if(msg.reply !== undefined)
38
  {
39
    var textAreaId = $("#text_reply").attr("name");
40
    if(width())
41
    {
42
      textAreaId = $(".text_icon #text_reply").attr("name");
43
    }
44
    if(msg.reply[0].id === textAreaId)
45
    {
46
      updateConversation(msg.reply);
47
    }
48
  }
49
50
  if (msg.Search !== undefined)
51
  {
52
    searchResult(msg.Search);
53
  }
54
55
  if (msg.Compose !== undefined)
56
  {
57
    composeResult(msg.Compose);
58
  }
59
};
60
61
62
// For First time
63
function init() {
64
  conn.send("OpenChat initiated..!");
65
}
66
67
// For updating Sidebar
68
function SideBar(msg)
69
{
70
  mobile("sidebar");
71
  // Getting Div
72
  if (msg != null)
73
  {
74
    createSidebarElement(msg);
75
  }
76
}
77
78
// SideBar Load Request
79
function sidebarRequest() {
80
  conn.send("Load Sidebar");
81
}
82
83
// Update Current Conversation
84
function updateConversation(data)
85
{
86
87
  if(!width())
88
  {
89
    sidebarRequest();
90
  }
91
92
  var ele = document.getElementById("conversation");
93
  ele.innerHTML = "";
94
95
    if (data[0].type === 1) {
96
      // For showing previous message
97
      if (data[0].load > 10) {
98
        var aElement = $("<a></a>").text("Show Previous Message!");
99
        var divElement = $("<div></div>").append(aElement);
100
        $("#conversation").append(divElement);
101
        $("#conversation div").addClass("previous");
102
        $("#conversation div a").attr({
103
          "onclick": "previous(this)",
104
          "id": data[0].username,
105
          "name": data[0].load
106
        });
107
      }
108
109
      for (var i = data.length - 1; i >= 1; i--) {
110
        // create element
111
        var divElement = document.createElement("div");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable divElement already seems to be declared on line 99. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
112
        if (data[i]["sent_by"] !== data[i].start)
113
        {
114
          divElement.setAttribute("class", "receiver");
115
        }
116
        else
117
        {
118
          divElement.setAttribute("class", "sender");
119
        }
120
121
        ele.appendChild(divElement);
122
        var brElement = document.createElement("br");
123
        brElement.setAttribute("style", "clear:both;");
124
        ele.appendChild(brElement);
125
126
        var pElement = document.createElement("p");
127
        var pText = document.createTextNode(data[i].message);
128
        pElement.appendChild(pText);
129
        divElement.appendChild(pElement);
130
131
        var h6Element = document.createElement("h6");
132
        var h6Text = document.createTextNode(data[i].time);
133
        h6Element.appendChild(h6Text);
134
        h6Element.setAttribute("class", "message_time");
135
        pElement.appendChild(h6Element);
136
      }
137
138
      setConversationDetails(data[0]);
139
140
      ele.scrollTop = ele.scrollHeight;
141
    }
142
    else
143
    {
144
      setConversationDetails(data[0]);
145
    }
146
}
147
148
149
function setConversationDetails(details)
150
{
151
  $("#chat_heading a").remove("a");
152
    var aElement = $("<a></a>").text(details.name);
153
    $("#chat_heading").append(aElement);
154
    $("#chat_heading a").attr({
155
      "href": "http://localhost/openchat/account.php/" + details.username
156
    });
157
158
    if (details.login_status === "1") {
159
      var online = document.createElement("p");
160
      online.setAttribute("class", "online");
161
      $("#chat_heading a").append(online);
162
      $("#chat_heading a p").css({
163
        "float": "right"
164
      });
165
    }
166
167
    if (width())
168
    {
169
      $(".text_icon #text_reply").attr({
170
        "name": details.id
171
      });
172
    }
173
    else
174
    {
175
      $("#text_reply").attr({
176
        "name": details.id
177
      });
178
    }
179
}
180
181
// Creating new Conversation or Loading Conversation
182
function newConversation(element, load)
183
{
184
  mobile("main");
185
  $("#compose_selection").css("visibility", "hidden");
186
  flag = 0;
187
  $("#compose_name").val("");
188
  $("#search_item").val("");
189
  $("#compose_text").hide();
190
191
  var msg = {
192
    "username": element.id,
193
    "load": load,
194
    "newConversation": "Initiated"
195
  };
196
  conn.send(JSON.stringify(msg));
197
198
}
199
200
// For reply to other messages
201
function reply()
202
{
203
  var replyElement = "";
204
  if (width())
205
  {
206
    replyElement = ".text_icon #text_reply";
207
  }
208
  else
209
  {
210
    replyElement = "#text_reply";
211
  }
212
213
  var message = [$(replyElement).val()];
214
  var id = $(replyElement).attr("name");
215
  $(replyElement).val("");
216
  // console.log(message);
217
  var q = {
218
    "name": id,
219
    "reply": message
220
  };
221
  conn.send(JSON.stringify(q));
222
223
}
224
225
// Compose new and direct message to anyone
226
function compose()
227
{
228
  mobile("compose");
229
  flag = 1;
230
  $("#chat_heading a").remove("a");
231
  document.getElementById("conversation").innerHTML = "";
232
  $("#compose_text").show();
233
}
234
235
function composeChoose()
236
{
237
  var text = document.getElementById("compose_name").value;
238
  if (text !== "") {
239
    var msg = {
240
      "value": text,
241
      "Compose": "Compose"
242
    };
243
    conn.send(JSON.stringify(msg));
244
  }
245
  else
246
  {
247
    $("#compose_selection").css("visibility", "hidden");
248
  }
249
}
250
251
//compose messages
252
function composeResult(arr)
253
{
254
  var ele = document.getElementById("suggestion");
255
  ele.innerHTML = "";
256
257
  if (arr !== "Not Found")
258
  {
259
    for (var i = arr.length - 1; i >= 0; i--)
260
    {
261
      var liElement = document.createElement("li");
262
      var aElement = document.createElement("a");
263
      var aText = document.createTextNode(arr[i].name);
264
      aElement.appendChild(aText);
265
      aElement.setAttribute("href", "#");
266
      aElement.setAttribute("onclick", "newConversation(this,10)");
267
      aElement.setAttribute("class", "suggestion");
268
      aElement.setAttribute("id", arr[i].username);
269
      liElement.appendChild(aElement);
270
      ele.appendChild(liElement);
271
    }
272
  }
273
  else
274
  {
275
    var aElement = $("<a></a>").text("Not Found");
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable aElement already seems to be declared on line 262. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
276
    var liElement = $("<li></li>").append(aElement);
0 ignored issues
show
Comprehensibility Naming Best Practice introduced by
The variable liElement already seems to be declared on line 261. Consider using another variable name or omitting the var keyword.

This check looks for variables that are declared in multiple lines. There may be several reasons for this.

In the simplest case the variable name was reused by mistake. This may lead to very hard to locate bugs.

If you want to reuse a variable for another purpose, consider declaring it at or near the top of your function and just assigning to it subsequently so it is always declared.

Loading history...
277
    $("#suggestion").append(liElement);
278
279
    $("#suggestion li a").attr({
280
      "onclick": "myFunction()"
281
    });
282
  }
283
  $("#compose_selection").css("visibility", "visible");
284
}
285
286
function search_choose()
287
{
288
  var text = $("#search_item").val();
289
  if (text !== "")
290
  {
291
    var msg = {
292
      "value": text,
293
      "search": "search"
294
    };
295
296
    conn.send(JSON.stringify(msg));
297
  }
298
  else
299
  {
300
    conn.send("Load Sidebar");
301
  }
302
303
}
304
305
function searchResult(arr)
306
{
307
  if (arr !== "Not Found")
308
  {
309
    createSidebarElement(arr);
310
  }
311
  else
312
  {
313
    $("#message").text("");
314
    var aElement = $("<a></a>").text("Not Found");
315
    $("#message").append(aElement);
316
    $("#message a").addClass("message");
317
  }
318
319
}
320
321
function createSidebarElement(data)
322
{
323
  // organising content according to time
324
  var ele = document.getElementById('message');
325
  ele.innerHTML = "";
326
  var condition = data.length;
327
  for (var i = 0; i < condition; i++)
328
  {
329
      var aElement = document.createElement("a"); //creating element a
330
      var aText = document.createTextNode(data[i].name);
331
      aElement.appendChild(aText);
332
      aElement.setAttribute("id", data[i].username);
333
      aElement.setAttribute("href", "message.php#" + data[i].username);
334
      aElement.setAttribute("class", "message");
335
      aElement.setAttribute("onclick", "newConversation(this,10)");
336
      ele.appendChild(aElement);
337
338
      // creating element span for showing time
339
      var spanElement = document.createElement("span");
340
      var spanText = document.createTextNode(data[i].time);
341
      spanElement.appendChild(spanText);
342
      spanElement.setAttribute("class", "message_time");
343
      aElement.appendChild(spanElement);
344
345
      if (data[i].login_status === "1") {
346
        var online = document.createElement("div");
347
        online.setAttribute("class", "online");
348
        aElement.appendChild(online);
349
    }
350
  }
351
}
352
353
window.ondblclick = myFunction;
354
355
function myFunction() // Hidden compose message input
356
{
357
  $("#compose_selection").css("visibility", "hidden");
358
  init();
359
  flag = 0;
360
  $("#compose_name").val("");
361
  $("#search_item").val("");
362
  $("#compose_text").hide();
363
}
364
365
function previous(element) // Load previous messages
366
{
367
  mobile("previous");
368
  var user = element.id;
0 ignored issues
show
Unused Code introduced by
The variable user seems to be never used. Consider removing it.
Loading history...
369
  var lo = element.name;
370
  newConversation(element, lo);
371
}
372
373
function mobile(ele)
374
{
375
  if (width()) {
376
    mob_hide();
377
    if (ele == "main") {
378
      $(".sidebar").hide();
379
      $(".mob-reply").show();
380
      $(".chat_name").show();
381
      $(".chat_name #chat_heading").show();
382
      if (pre == "") {
383
        $(".main div").remove("div");
384
        $(".main br").remove("br");
385
        $(".chat_name #chat_heading a").remove("a");
386
      }
387
      $(".main").show();
388
    }
389
    if (ele == "compose") {
390
      $(".chat_name").show();
391
      $(".chat_name .compose_text").show();
392
      $(".sidebar").hide();
393
      $("#compose_selection").show();
394
    }
395
    if (ele == "sidebar") {
396
      $(".sidebar").show();
397
    }
398
    if (ele == "previous")
399
    {
400
      pre = "1";
401
    }
402
    else
403
    {
404
      pre = "";
405
    }
406
  }
407
}
408
409
function show_search() {
410
  // console.log("HE0");
411
  mob_hide();
412
  $(".search_message").show();
413
  $(".sidebar").show();
414
}
415
416
function mob_hide() {
417
  $(".search_message").hide();
418
  $(".sidebar").hide();
419
  $(".main").hide();
420
  $(".chat_name").hide();
421
  $(".mob-reply").hide();
422
}
423
424
function width() {
425
  if (window.innerWidth < 500)
426
  {
427
    return true;
428
  }
429
  return false;
430
}
431
432
// Audio Recognization
433
434
function startDictation() {
435
436
  if (window.hasOwnProperty("webkitSpeechRecognition")) {
437
438
    var recognition = new webkitSpeechRecognition();
0 ignored issues
show
Bug introduced by
The variable webkitSpeechRecognition seems to be never declared. If this is a global, consider adding a /** global: webkitSpeechRecognition */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
Coding Style Best Practice introduced by
By convention, constructors like webkitSpeechRecognition should be capitalized.
Loading history...
439
440
    recognition.continuous = false;
441
    recognition.interimResults = false;
442
443
    recognition.lang = "en-IN";
444
    recognition.start();
445
446
    recognition.onresult = function(e) {
447
      document.getElementById("text_reply").value = e.results[0][0].transcript;
448
      recognition.stop();
449
      reply();
450
    };
451
452
    recognition.onerror = function() {
453
      recognition.stop();
454
    }
455
456
  }
457
}
458
459
console.log("Hello, Contact me at [email protected]");
0 ignored issues
show
Debugging Code introduced by
console.log looks like debug code. Are you sure you do not want to remove it?
Loading history...